<template>
	<view class="navbars bg-white">
		<!-- 导航 -->
		<view class="navbar" :class="showNavFloat ? 'floatbar' : ''">
			<view class="menu" v-if="list.length > 0">
				<view class="category">
					<scroll-view :scroll-x="true" :scroll-with-animation="true" :scroll-into-view="scroll_category_id" @scroll="navFloatShow">
						<view
							class="item"
							v-for="(item, index) in list"
							:key="index"
							:class="active == index ? 'current' : ''"
							:id="'category_id-' + index"
							:style="'width:' + (list.length <= 4 ? 100 / list.length + '%' : '')"
							@tap="handleCategoryChange(item.id, index)"
						>
							<view class="text">
								<text>{{ item.name }}</text>
							</view>
						</view>
					</scroll-view>
				</view>
				<view class="list" @tap="handleMenuShow"><text class="cuIcon-cascades"></text></view>
			</view>
		</view>
		<view class="list-content">
			<!-- 	<view v-if="menusList.length>0" class='flex flex-wrap margin-left'>
				<view @tap="handleCategoryChange2(item.id, index)" class="padding-xs" v-for="(item,index) in menusList" :key="index">
					<view :class="activeMenu == index ? 'line-blue' : 'line-gray'" class='cu-tag round '>{{item.name}}</view>
				</view>
			</view> -->
			<view class="menu-block fade-in" v-show="showMenu">
				<view class="list">
					<text
						class="item"
						v-for="(item, index) in props.list"
						:key="index"
						:class="active == index ? 'current' : ''"
						:id="'category_id-' + (index + 1)"
						@tap="handleCategoryChange(item.id, index)"
					>
						{{ item.name }}
					</text>
				</view>
			</view>
			<view v-if="menusList.length > 0"><cu-classfly @action="handleCategoryChangeMenu" :list="menusList"></cu-classfly></view>
			<scroller v-else @init="initScroller" @down="refreshData" @up="getData" :up="optUp" @scroll="navFloatShow" :fixed="false"><slot name="content"></slot></scroller>
		</view>
	</view>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import cuClassfly from '@/components/list/cu-classfly.vue';
const emit = defineEmits(['action', 'load']);
const props = defineProps({
	list: {
		default: () => {
			return [
				{
					id: 0,
					name: '',
					children: []
				}
			];
		}
	},
	value: {
		type: Number,
		default: 0
	}
});

const active = ref(props.value);
const activeMenu = ref(-1);
const showMenu = ref(false);
const showNavFloat = ref(false);
const menusList = ref([]);
const optUp = {
	auto: true,
	onScroll: true,
	page: {
		size: 20
	},
	empty: {
		tip: '暂无文章~'
	}
};
const scroll_category_id = ref('scroll_category_id_0');
const scroller = ref();
watch(
	() => props.value,
	(newValue, oldValue) => {
		active.value = newValue;
		menusList.value = props.list[newValue].children;
	}
);
watch(
	() => props.list,
	(newValue, oldValue) => {
		if (props.list[props.value].children) {
			menusList.value = props.list[props.value].children;
		}
	}
);
const handleMenuShow = () => {
	showMenu.value = !showMenu.value;
};
const handleCategoryChange = (e: any, index: any) => {
	active.value = index;
	activeMenu.value = -1;
	menusList.value = props.list[index].children;
	showMenu.value = false;
	emit('action', e);
};
const handleCategoryChange2 = (e: any, index: any) => {
	activeMenu.value = index;
	emit('action', e);
};
const handleCategoryChangeMenu = (e: any) => {
	activeMenu.value = e.id;
	menusList.value = [];
	emit('action', e.id);
};
/*滚动时导航栏浮动*/
const navFloatShow = () => {
	if (scroller.value) {
		if (scroller.value.scrollTop > 60) {
			if (!showNavFloat.value) {
				showNavFloat.value = true;
			}
		} else {
			if (showNavFloat.value) {
				showNavFloat.value = false;
			}
		}
	}
};
/*初始化滚动*/
const initScroller = (scroller: any) => {
	scroller.value = scroller;
};

/*刷新数据*/
const refreshData = () => {
	uni.showLoading({
		title: '刷新中'
	});
	scroller.value.resetUpScroll();
};
const getData = () => {
	emit('load');
};
</script>

<style lang="scss" scoped>
/*浮动navbar*/
.floatbar {
	//border-bottom: 1rpx solid #e8e8e8;
	box-shadow: 0px 2px 2px -2px #e8e8e8;
	padding-bottom: 12rpx;
}

/* 顶部navbar */
.navbar {
	/*分类*/
	.menu {
		position: relative;
		height: 70rpx;
		white-space: nowrap;
		padding: 15rpx 0 6rpx;
		z-index: 10;
		display: flex;
		align-items: center;
		justify-content: space-between;

		/*分类*/
		.category {
			width: 650rpx;
			margin-left: 20rpx;
			white-space: nowrap;
			position: relative;

			scroll-view {
				width: auto;

				.item {
					position: relative;
					display: inline-block;
					margin: 0 31rpx 0;
					height: 70rpx;
					text-align: center;
					padding-top: 7rpx;

					//line-height: 80rpx;
					&:first-child {
						margin-left: 10rpx;
					}

					&:after {
						content: '';
						width: 0;
						height: 0;
						position: absolute;
						left: 50%;
						bottom: 0;
						transform: translateX(-50%);
						transition: 0.3s;
					}

					.text {
						position: relative;
						width: auto;
						height: auto;
						line-height: auto;
						display: inline-block;

						text {
							font-size: 28rpx;
							font-family: PingFangSC-Medium, PingFang SC;
							font-weight: 500;
							color: #666666;
							line-height: 40rpx;
							letter-spacing: 2rpx;
						}

						image {
							position: absolute;
							top: 16rpx;
							right: -14rpx;
							width: 50rpx;
							height: 50rpx;
							display: none;
						}
					}
				}

				.current {
					&:after {
						width: 50%;
					}

					.text {
						text {
							font-size: 28rpx;
							font-family: PingFangSC-Medium, PingFang SC;
							font-weight: 700;
							color: #0660c9;
							line-height: 40rpx;
							letter-spacing: 2rpx;
						}

						image {
							display: block;
						}

						border-bottom: 6rpx solid #0660c9;
					}
				}
			}
		}

		.list {
			width: 70rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			//box-shadow: -4rpx 0 0 #e9ebee;
			//box-shadow: -2px 0 0 #262626;
			v-deep::icon {
				font-size: 36rpx;
				margin-top: -6rpx;
			}
		}
	}
}

.list-content {
	position: relative;
}

.menu-block {
	position: absolute;
	top: 0;
	left: 0;
	background: #fff;
	border-bottom: 1rpx solid #f5f5f5;
	padding: 20rpx 0 50rpx;
	z-index: 100;
	width: 100%;

	//box-shadow: 0 15rpx 10rpx -15rpx #e9ebee;
	.list {
		padding-left: 10rpx;

		text {
			background: #f5f7fa;
			border-radius: 8rpx;
			font-size: 32rpx;
			margin: 12rpx 20rpx;
			display: inline-block;
			height: 72rpx;
			width: 144rpx;
			line-height: 72rpx;
			text-align: center;
		}

		.current {
			color: #fff;
			background: #8cc7b5;
			//font-size: 32rpx;
		}
	}
}
</style>
